/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.basecommerce.model.AddressLine1Attribute;
import de.hybris.platform.basecommerce.model.AddressLine2Attribute;
import de.hybris.platform.core.model.c2l.CountryModel;
import de.hybris.platform.core.model.c2l.RegionModel;
import de.hybris.platform.core.model.user.AddressModel;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.AddressDAO;
import com.hand.cloud.b2b.model.EPAddressModel;


/**
 * @author StephenWang
 * @time Aug 5, 2014 1:35:53 PM
 * 
 */
@Component(value = "addressDAO")
public class DefaultAddressDAO implements AddressDAO
{

	@Autowired
	private ModelService modelService;

	@Autowired
	private FlexibleSearchService flexibleSearchService;


	/**
	 * 新生成地址信息
	 * 
	 * @return
	 */
	@Override
	public AddressModel createAddress(EPAddressModel epAddress)
	{
		AddressModel address = this.modelService.create(AddressModel.class);
 
		// 如果对象有动态字段，一定要将动态字段初始化
		AddressLine1Attribute line1 = new AddressLine1Attribute();
		AddressLine2Attribute line2 = new AddressLine2Attribute();
		line1.set(address, "line1");
		line2.set(address, "line2");

		/*address.setCellphone(epAddress.getReceiver_mobile());
		address.setDistrict(epAddress.getReceiver_district());
		address.setTown(epAddress.getReceiver_city());
		address.setStreetname(epAddress.getReceiver_address());

		address.setPostalcode(epAddress.getReceiver_zip());
		address.setPhone1(epAddress.getReceiver_phone());
		address.setFirstname(epAddress.getReceiver_name());*/
		
		address.setCellphone(epAddress.getReceiverAddress());
		address.setDistrict(epAddress.getReceiverDistrict());
		address.setTown(epAddress.getReceiverCity());
		address.setStreetname(epAddress.getReceiverAddress());
		
		address.setPostalcode(epAddress.getReceiverZip());
		address.setPhone1(epAddress.getReceiverPhone());
		address.setFirstname(epAddress.getReceiverName());

		// 国家全部默认为中国
		CountryModel country = this.getCountry("CN");
		//RegionModel region = this.createRegion(epAddress.getReceiver_state(), country);

		RegionModel region = this.getRegionByCode("CN-32", country);

		address.setCountry(country);
		address.setRegion(region);

		address.setOwner(country);

		this.modelService.save(address);
		return address;
	}

	@Override
	public RegionModel getRegionByCode(String isoCode, CountryModel country)
	{

		RegionModel region = this.modelService.create(RegionModel.class);

		region.setActive(Boolean.TRUE);
		region.setIsocode(isoCode);
		region.setCountry(country);

		try
		{
			region = this.flexibleSearchService.getModelByExample(region);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return region;
	}

	/**
	 * 创建区域（省）
	 * 
	 * @param regionName
	 * @param country
	 * @return
	 */
	@Override
	public RegionModel createRegion(String regionName, CountryModel country)
	{
		RegionModel region = this.modelService.create(RegionModel.class);

		region.setActive(Boolean.TRUE);
		region.setCreationtime(new Date());
		region.setName(regionName);
		region.setCountry(country);
		region.setIsocode("CN");

		this.modelService.save(region);
		return region;
	}

	/**
	 * 通过国标码获取国家信息
	 * 
	 * @param isoCode
	 * @return
	 */
	@Override
	public CountryModel getCountry(String isoCode)
	{
		CountryModel country = new CountryModel();
		country.setActive(Boolean.TRUE);
		country.setIsocode(isoCode);

		try
		{
			country = this.flexibleSearchService.getModelByExample(country);
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return country;
	}
}
